﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.GuessTheWords2.Core;
using Studio77.GuessTheWords2.Entities;
using Studio77.GuessTheWords2.Pages;
using Studio77.GuessTheWords2.Util;

namespace Studio77.GuessTheWords2.Views
{
    public partial class ClassifyItem : UserControl
    {
        public int ClassIndex { get; set; }

        public ClassifyItem()
        {
            InitializeComponent();
            this.Loaded += ClassifyItem_Loaded;

        }

        void ClassifyItem_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= ClassifyItem_Loaded;
            this.txtClassIndex.Text = ClassIndex.ToString();
        }

        public void ShowContent()
        {
            int type = StageManager.Instance.GetClassStatus(ClassIndex);
            if (type == 1)
            {
                this.btnNormal.Visibility = System.Windows.Visibility.Visible;
                this.imgLock.Visibility = System.Windows.Visibility.Collapsed;
                this.imgUnable.Visibility = System.Windows.Visibility.Collapsed;
                this.txtComplateStar.Visibility = System.Windows.Visibility.Visible;
                this.txtTotalTime.Visibility = System.Windows.Visibility.Visible;

                // 总的游戏时间
                int totalSeconds = 0;
                int totalStar = 0;
                for (int i = 1; i <= GameStatics.StageCount; i++)
                {
                    String key = ClassIndex + "_" + i;
                    if (StageManager.Instance.StageDatas.ContainsKey(key))
                    {
                        StageData sd = StageManager.Instance.StageDatas[key];
                        if (sd.AllWordCount > 0)
                        {
                            totalSeconds += sd.TotalSeconds;
                            totalStar += Misc.GetStar(sd.ComplateCount
                                    / (float)sd.AllWordCount);
                        }
                    }
                }
                this.txtComplateStar.Text = totalStar + "/150";
                this.txtTotalTime.Text = getUseTimeStr(totalSeconds);
            }
            else if (type == 2)
            {
                this.btnNormal.Visibility = System.Windows.Visibility.Collapsed;
                this.imgLock.Visibility = System.Windows.Visibility.Visible;
                this.imgUnable.Visibility = System.Windows.Visibility.Collapsed;
                this.txtComplateStar.Visibility = System.Windows.Visibility.Collapsed;
                this.txtTotalTime.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnNormal.Visibility = System.Windows.Visibility.Collapsed;
                this.imgLock.Visibility = System.Windows.Visibility.Collapsed;
                this.imgUnable.Visibility = System.Windows.Visibility.Visible;
                this.txtComplateStar.Visibility = System.Windows.Visibility.Collapsed;
                this.txtTotalTime.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private String getUseTimeStr(long totalSeconds)
        {
            return (totalSeconds / 60) + "'"
                    + (totalSeconds % 60).ToString("D2");
        }

        private void btnNormal_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            StageManager.Instance.CurrentClass = ClassIndex;
            //进入关卡选择界面
            ClassifyPage.Instance.NavigationService.Navigate(new Uri("/Pages/StagePage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}
